﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PhoneStore.Presentation.CustomControl;
using PhoneStore.Entity;

namespace PhoneStore.Presentation
{
    /// <summary>
    /// Interaction logic for AdvanceSearch.xaml
    /// </summary>
    public partial class AdvanceSearch : WindowBase
    {
        public AdvanceSearch()
        {
            InitializeComponent();
            cbbBlueTooth.Property = x => x.BlueTooth;
            cbbCamera.Property = x => x.Camera;
            cbbGPS.Property = x => x.GPS;
            cbbMemory.Property = x => x.MemoryCard;
            cbbMusic.Property = x => x.Music;
            cbbQWERTY.Property = x => x.QWERTY;
            cbbRadio.Property = x => x.FMRadio;
            cbbSims.Property = x => x.MultiSim;
            cbbTouch.Property = x => x.TouchScreen;
            cbbTV.Property = x => x.TV;
            cbbVoice.Property = x => x.VoiceRecord;
            cbbWifi.Property = x => x.Wifi;
            cbbManufacturer.Property = x => x.Product.Manufacturer;
            SetUpCbbData();
            PhoneSource = null;

        }
        void SetUpCbbData()
        {
            listCbb = new List<PhoneComboBox>
            {
                cbbBlueTooth,
                cbbCamera,
                cbbGPS,
                cbbMemory,
                cbbMusic,
                cbbQWERTY,
                cbbRadio,
                cbbSims,
                cbbTouch,
                cbbTV,
                cbbVoice,
                cbbWifi,
                cbbManufacturer,
            };
            foreach (var i in listCbb)
            {
                var src = DAO.GetPhonePropertyValues(i.Property);
                src.Add("<Tất Cả>");
                i.ItemsSource = src;
                i.SetCurrentValue(PhoneComboBox.SelectedValueProperty, "<Tất Cả>");  
            }
        }
        List<PhoneComboBox> listCbb { get; set; }
        public IQueryable<Phone> PhoneSource{get;set;}

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DienThoaiEntities ae = new DienThoaiEntities();
            PhoneSource = ae.Phones.Include("Product") as IQueryable<Phone>;
            foreach (var i in listCbb)
            {
                if (i == cbbManufacturer)
                {
                    if (i.Text != "<Tất Cả>")
                    {
                        PhoneSource = PhoneSource.Where(x => x.Product.Manufacturer == i.Text);
                    }
                }
                else
                {
                    PhoneSource = PhoneSource.Where(i.GetSearchExpression());
                }
            }
            this.Close();
        }
    }
}
